package com.sundhage.gui {
	import flash.display.*;
	import flash.events.*;

	public class JSDropdown extends Sprite {
		
		public static const ITEMCLICK:String = "jsdditemclick";
		public static const PUTMEONTOP:String = "jsddputmeontop";
		
		private var settings:JSDropdownSettings;
		private var assets:JSDropdownAssets;
		//private var list:Sprite;
		private var headLayer:Sprite;
		private var listLayer:Sprite;
		private var listLayerBkg:Sprite;
		private var items:Array;
		public var lastClickedData:Object;
		private var headTitle:JSText;
		
		private function getNextY():int {
			var yoffs:int = 0;
			for (var i:int = 0; i<items.length; i++)
				yoffs += items[i].height+settings.itemPad;
				
			return yoffs;
		}
		
		private function update(item:JSListItem):void {
			//trace("Sätt vald: "+item.getTitle());
			headTitle.text = item.getTitle();
		}
		
		private function itemClick(e:Event):void {
			var cItem:JSListItem = e.target as JSListItem;
			if (lastClickedData != cItem.data) {
				lastClickedData = cItem.data;
				dispatchEvent(new Event(ITEMCLICK));
				update(cItem);
			}
			hideList();
		}
		
		public function addItem(title:String, data:Object, down:Boolean = true):void {
			var lsettings:JSListSettings = new JSListSettings(settings.orgObj);
			
			var lassets:JSListAssets = new JSListAssets(assets.orgObj);
			
			var item:JSListItem = new JSListItem(title, items.length, lsettings, lassets, null, null, undefined, data, JSListItem.ITEM, -1);
			
			if (down)
				item.y = getNextY();
			else
				item.y = -getNextY()-item.height*2-settings.itemPad;
					
			item.addEventListener(JSListItem.CLICK, itemClick);
			items.push(item);
		}
		
		public function render():void {
			for (var i:int = 0; i<items.length; i++) {
				try { listLayer.removeChild(items[i]); } catch (e:Error) {}
				listLayer.addChild(items[i]);
			}
		}
		public function setStartId(id:int):void {
			lastClickedData = items[id];
			update(items[id]);
			
		}
		private function hideList(e:Event = null):void {
			listLayer.visible = false;
		}
		
		private function showList(e:Event = null):void {
			parent.addChild(this);
			dispatchEvent(new Event(PUTMEONTOP));
			listLayer.visible = true;
		}
		
		private function onClick(e:Event):void {
		/*
			if (this.parent.mouseX < this.x) hideList();
			if (this.parent.mouseX > this.x+this.width) hideList();
			if (this.parent.mouseY < this.y) hideList();
			if (this.parent.mouseY > this.y+this.width) hideList();
		*/
			
			if (this.parent.mouseX < this.x ||
				this.parent.mouseX > this.x+this.width ||
				this.parent.mouseY < this.y ||
				this.parent.mouseY > this.y+this.width) hideList();
		}
		
		private function initMouse(e:Event):void {
			stage.addEventListener(MouseEvent.CLICK, onClick);
		}
		public function JSDropdown(settings:JSDropdownSettings, assets:JSDropdownAssets) {
			this.settings = settings;
			this.assets = assets;
			addEventListener(Event.ADDED_TO_STAGE, initMouse);
			headLayer = new Sprite();
			headLayer.addEventListener(MouseEvent.CLICK, showList);
			
			headTitle = new JSText(settings.title, settings.headTextFormat, settings.headTextColor, true, false);

			var hBkg:Sprite = assets.getBackground();
			hBkg.width = settings.width;
			hBkg.height = headTitle.height;
			
			headLayer.addChild(hBkg);
			headLayer.buttonMode = true;
			
			headLayer.addChild(headTitle);
		
			listLayer = new Sprite();
			listLayer.visible = false;
			listLayer.y = headLayer.height+settings.itemPad;
			addChild(headLayer);
			addChild(listLayer);
			items = new Array();
			//this.y = 30;
			
			
		}
		
	}
}